// Pages/index/index.js
var getlog=require("../../utils/judgelog.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    /*分类滚动栏的数据 */
    chooseData: [{ 'index': '1', 'dataName': '售卖信息'},{'index':2,'dataName':'需求信息'}],
    pageIndexs: [],// 当前分类当前的页数
    isLastPages: [],// 当前分类是否是尾页
    pageData: [1, 2],//page数据
    activeIndex: 0,//当前页面索引
    index:0,
    toView: 'view0',
    boundsArray: [],
    idchange:0,



    // colorArray: ['lightcoral', 'lightgreen', 'lightskyblue', 'lightsalmon', 'lightyellow', 'lightseagreen', 'lightcoral', 'lightgreen', 'lightskyblue', 'lightsalmon', 'lightyellow', 'lightseagreen', 'lightcoral', 'lightgreen', 'lightskyblue', 'lightsalmon', 'lightyellow', 'lightseagreen'],   
    /*分类数据 */
    typeData: [{ 'index': '1', 'typeName': '数码' }, { 'index': '2', 'typeName': '办公' }, { 'index': '3', 'typeName': '男装' }, { 'index': '4', 'typeName': '女装' }, { 'index': '5', 'typeName': '美妆' }, { 'index': '6', 'typeName': '鞋子' }, { 'index': '7', 'typeName': '箱包' }, { 'index': '8', 'typeName': '书籍' }, { 'index': '9', 'typeName': '其他' },],
    pageDataType: [1, 2,3,4,5,6,7,8,9,10,11],//page数据
    activeIndexType: 0,//当前页面索引
    indexType: 0,
    toViewType: 'view0',
  
    dataArray:[],

    searchdata:'',
    focus:true,



    swphoto:[],
    swdata:[]


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
      that.showInit();
      // that.talking();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getsw()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  search:function(e){
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  tabClick: function (e) {//处理点击售卖信息和需求信息时的索引，改变wxss
    console.log(e)
    var that = this;
    var index = 0;
    for (var i = 0; i < this.data.chooseData.length; i++) {
      if (this.data.chooseData[i].index === e.currentTarget.dataset.item.index) {
        index = i
       
        break
      }
    }
  
    that.setData({
      activeIndex: index,//设置当前索引
      
    })

    if (index > 0) {
      that.setData({
        toView: 'view' + (index - 1)
      })
    }
  },
  //视图滑动事件1 售卖和需求分开的时候
  bindChange: function (e) {
    var that = this
    var current = e.detail.current;

    this.setData({
      activeIndex: current,
      index: current,
      toView: 'view' + parseInt(current)
    });

    that.showInit();
  },


  typeClick: function (e) {//分类，改变wxss
    console.log(e)
    var that = this;
    var index = 0;
    for (var i = 0; i < this.data.typeData.length; i++) {
   
      if (this.data.typeData[i].index === e.currentTarget.dataset.item.index) {
        index = i
        console.log(index)
        break
      }
    }

    that.setData({
      activeIndexType: index,//设置当前索引
    })
    console.log(this.data.activeIndex+''+this.data.activeIndexType)
    if (index > 0) {
      that.setData({
        toViewType: 'view' + (index - 1)
      })
    }
  },

  bindTypeChange: function (e) {//数据展示
    console.log("change--------------------------------------")
    var that = this
    var current = e.detail.current;
    var api = "http://47.103.21.65:5645/sell/getSellInfoByType";

    if(that.data.activeIndex==1){
      api ="http://47.103.21.65:5645/require/getRequireListByType";
    }


    this.setData({
      activeIndexType: current,
      indexType: current,
      toViewType: 'view' + parseInt(current)
    });
 wx.request({
      url: api,
      method: "POST",

      data: {  //参数传递
        typeId:current+1,
        page:1
      },
      header: { //设置请求头
        'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，
       
      },

      success: function (res) { 
        console.log(res.data);
        that.setData({
          dataArray: res.data.data.info
        })
        console.log(res.data.info)
    
      },
      fail: function (error) { //请求失败
        console.log(error);
       
      }
    })
  },
  showInit: function () {//数据展示
    
  },
  jumpto: function (options){
    /**页面跳转 */
    var flag=-1;
    getlog.getloginmode(function(result){
        flag=result;

      if (flag == -1 || flag == 0) {
        /**未登录 */
        console.log(flag)
        wx.showToast({
          title: '请登录后查看详细信息',
          icon: 'none'
        })
      } else {
        console.log(options.currentTarget.dataset.str);
        console.log(options);
        var thingId = JSON.stringify(options.currentTarget.dataset.str);

        wx.navigateTo({
          url: '../detailPageSell/detailPageSell?thingsId=' + thingId,
        })
      }
    })
    
  },
  searchInit:function(e){
     var that=this;
     
     that.setData({
       focus:false
     })
    wx.navigateTo({
      url: '../search/search?searchdata='+that.data.searchdata,
    })
  }
  , 
  searchInput:function(e){
    this.setData({searchdata:e.detail.value})
  },
  todetail:function(e){/**需求 */
    var flag = -1;
    getlog.getloginmode(function (result) {
      flag = result;

      if (flag == -1 || flag == 0) {
        /**未登录 */
        console.log(flag)
        wx.showToast({
          title: '请登录后查看详细信息',
          icon: 'none'
        })
      } else {
    wx.navigateTo({
      url: '../requiredetail/requirement_detail?id=' + e.currentTarget.dataset.id,
    })
    }
    })
  },
  getsw(){
    var that=this
    wx.request({
      url: 'http://10.172.219.56/mynews/index/getnewslist',
      method:"get",
      data:{page:1},
      success:function(res){
        console.log(res.data.list[0])
        var s = res.data.list
          that.setData({
            swphoto: s,
            swdata:s,
          })
      },
      fail:function(res){
        console.log(res)
      }

    })
  },
 
  
})
